Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variabl

Posted by Thomas on Stack Overflow See other posts from Stack Overflow or by Thomas
Published on 2010-04-26T12:59:53Z Indexed on 2010/04/26 13:03 UTC
Read the original article Hit count: 1178

Filed under:
|
|
|
|

Hi, I'm working with PHP PDO and I have the following problem:

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in /var/www/site/classes/enterprise.php on line 63

Here is my code:

public function getCompaniesByCity(City $city, $options = null) {

$database = Connection::getConnection();

if(empty($options)) { $statement = $database->prepare("SELECT * FROM empresas WHERE empresas.cidades_codigo = ?"); $statement->bindValue(1, $city->getId()); } else { $sql = "SELECT * FROM empresas INNER JOIN prods_empresas ON prods_empresas.empresas_codigo = empresas.codigo WHERE ";

foreach($options as $option) { $sql .= 'prods_empresas.produtos_codigo = ? OR '; }

$sql = substr($sql, 0, -4); $sql .= ' AND empresas.cidades_codigo = ?';

$statement = $database->prepare($sql);

echo $sql;

foreach($options as $i => $option) { $statement->bindValue($i + 1, $option->getId()); }

$statement->bindValue(count($options), $city->getId()); }

$statement->execute();

$objects = $statement->fetchAll(PDO::FETCH_OBJ); $companies = array();

if(!empty($objects)) { foreach($objects as $object) { $data = array( 'id' => $object->codigo, 'name' => $object->nome, 'link' => $object->link, 'email' => $object->email, 'details' => $object->detalhes, 'logo' => $object->logo );

$enterprise = new Enterprise($data);
array_push($companies, $enterprise);

}

return $companies; } }

Thank you very much!

© Stack Overflow or respective owner

Related posts about php

Related posts about database